<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/sample.js"></script>
    <script type="text/javascript" src="../lib/ext_ux_ckeditor.js"></script>
    <script type="text/javascript" src="../js/dict_store.js"></script>
    <script type="text/javascript" src="../lib/ext/plugins/clearButton/ClearButton.js" ></script>
    <link rel="stylesheet" type="text/css" href="../lib/ext/plugins/clearButton/ClearButton.css" />
</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});
    var dataForm;
    var data = {};
    Ext.onReady(function () {
        //保存数据
        function doAdd(){
            if(!dataForm.form.isValid()){
                Ext.Msg.alert('系统提示','请修正指出错误，检查输入是否正确!');
                return;
            }
            var params = dataForm.form.getFieldValues();
            if(params && params.init_date){
                params.init_date = Ext.util.Format.date(params.init_date,'Ymd')
            }
            var _url = '/wx/srcmgt/add';
            if(params && params._id ){
                _url = '/wx/srcmgt/edit';
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    alert('业务处理成功');
                    parent.refresh();
                    parent.closeWin();
                },
                failure: function(response, opts) {
                    alert('业务处理失败');
                }
            });

        }
        //关闭窗口
        function doClose(){
            parent.closeWin();
        }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
            {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];
        dataForm = new Ext.FormPanel({
            layout : 'fit',
            items: [
                {
                    xtype: 'form',
                    defaultType: 'textfield',
                    layout: {type: 'table',columns:1},
                    items: [
                        {
                            fieldLabel: '产品编号',
                            name : '_id',
                            hidden : true
                        },
                        {
                            fieldLabel: '素材类型',
                            name : 'type',
                            value : 'text',
                            hidden : true
                        },
                        {
                            fieldLabel: '标题',
                            name : 'title',
                            allowBlank: false
                        },
                        {
                            fieldLabel : '日期',
                            name : 'init_date',
                            xtype : 'datefield',
                            editable  : false,
                            format: 'Y/m/d',
                            value : new Date(),
                            plugins: ['clearbutton']
                        },
                        {
                            xtype: 'combobox',
                            fieldLabel: '所属栏目',
                            id : "category",
                            name : 'category',
                            queryMode: 'local',
                            displayField: 'key_name',
                            valueField: 'key_code',
                            allowBlank: false,
                            editable : false,
                            store:  dict_store
                        },

                        {xtype : "label",text: '内容'},
                        {
                            xtype : 'textareafield',
                            name : 'content',
                            fieldLabel : '',
                            width : '100%'
                        }
                    ],
                    tbar : tbar
                }
            ]
        });

        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });
        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/wx/srcmgt/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);

                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        } else{
            init_form();
        }

        function init_form(){
            data = data || {};
            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
            if(data.init_date){
                dataForm.form.findField('init_date').setValue(Ext.Date.parse(data.init_date,'Ymd'));
            }
            //下拉框的加载
            dict_store.load({params:{parent_code : "wx_src_text"},callback:function(){
                dataForm.form.findField('category').setValue(data.category);
            }});

        }
    });
</script>
<body>
<div id="mypanel" style="height:1024px;width:100%"></div>
</body>
</html>